package com.ljcalc.business.components;


import java.util.*;

import junit.framework.TestCase;

import com.ljcalc.business.components.requests.HistoryResultRequest;
import com.ljcalc.business.components.requests.SSLHistoryResultRequest;

public class HistoryResultAnalyzerTest extends TestCase {

	public void setUp() throws Exception{
		
	}
	
	public void tearDown() throws Exception{
		
	}
	
	public void testGetThisYears2011SimilarityStat(){
		//starts from Wednesday 2011 first week
		System.out.println("testGetThisYears2011SimilarityStat STARTS");
		HistoryResultRequest sslhrr = new SSLHistoryResultRequest(SSLHistoryResultRequest.ONSDAG_TYPE, 2011, 1) ;
		List<Integer> nList = new LinkedList<Integer>();
		do{
			List<Combination> list=  SSLHistoryResultsAnalyzer.getInstance().getResult(sslhrr.getType(), sslhrr.getYear(), sslhrr.getRound());

			Iterator<Combination> itr = list.iterator();
			while (itr.hasNext()) {
				Combination comb = (Combination) itr.next();			
				HashMap map = SSLHistoryResultsAnalyzer.getInstance().getHistorySimilarities(comb,sslhrr, 6);
				nList.add(new Integer(map==null?0:map.size()));
	//			System.out.println(comb);
			}
		}while(SSLHistoryResultsAnalyzer.getInstance().isValidRequest(sslhrr= SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(sslhrr)));
		
		double totalSample = nList.size();
		int counter = 0;
		for (int i = 0; i < nList.size(); i++) {
			counter += nList.get(i).intValue();			
		}
		System.out.println(nList);
		
		System.out.println((double)(counter/totalSample));
		
		System.out.println("testGetThisYears2011SimilarityStat ENDS");
		
	}
	
	public void testGetAnaylzerForTheLatestResult(){
		HistoryResultRequest hrr = SSLResultHistories.getInstance().getLatestResultInRequest();
		
		
		List<Combination> list=  SSLHistoryResultsAnalyzer.getInstance().getResult(hrr.getType(), hrr.getYear(), hrr.getRound());
		Iterator<Combination> itr = list.iterator();
		while (itr.hasNext()) {
			Combination comb = (Combination) itr.next();
			comb.setHasSameCombInHistory(SSLHistoryResultsAnalyzer.getInstance().hasSameCombInHistory(comb));

			comb.setisConsecutiveAcceptableCombination(SSLHistoryResultsAnalyzer.getInstance()
							.isConsecutiveAcceptableCombination(comb));
			comb.setInTimeConsecutiveTolerance(SSLHistoryResultsAnalyzer.getInstance().isInTimeConsecutiveTolerance(comb));
			
			comb.setHistorySimilarities(SSLHistoryResultsAnalyzer.getInstance().getHistorySimilarities(comb,5));
			
			System.out.println(comb);
		}

	}
	
	public void testGetLeastNumberTimeConsecutiveStat(){
		System.out.println(SSLHistoryResultsAnalyzer.getInstance().getNumberTimeConsecutiveStat(0));
	}
	
	public void testGetMaxNumberTimeConsecutiveStat(){
		System.out.println(SSLHistoryResultsAnalyzer.getInstance().getNumberTimeConsecutiveStat(6));
	}
	
	public void testGetMidNumberTimeConsecutiveStat(){
		System.out.println(SSLHistoryResultsAnalyzer.getInstance().getNumberTimeConsecutiveStat(3));
	}
	
	public void testGetLottoStat(){
		
		System.out.println(SSLHistoryResultsAnalyzer.getInstance().getLottoStat());
	}
	
	public void testGetAdjacentStat(){

		
		HashMap<String, ConsecutiveSpacePattern> ret2 = SSLHistoryResultsAnalyzer.getInstance().getHistoryResultsStat();
//		System.out.println(ret2);		
	}
	

	
	public void AtestGetNextResultRequest(){
		
		GregorianCalendar d = new GregorianCalendar();
		d.setTime(new Date(System.currentTimeMillis()));
		int wofy =  d.get(Calendar.WEEK_OF_YEAR);
		HistoryResultRequest r = new SSLHistoryResultRequest(SSLHistoryResultRequest.LORDAG_TYPE, 2010, 52);
		r = SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(r);
		assertEquals(SSLHistoryResultRequest.ONSDAG_TYPE, r.getType());
		assertEquals(2011, r.getYear());
		assertEquals(1, r.getRound());
		

		assertEquals(false, SSLHistoryResultsAnalyzer.getInstance().isValidRequest(new SSLHistoryResultRequest(SSLHistoryResultRequest.ONSDAG_TYPE, 2011,wofy)));
		assertEquals(true, SSLHistoryResultsAnalyzer.getInstance().isValidRequest(new SSLHistoryResultRequest(SSLHistoryResultRequest.ONSDAG_TYPE, 2011, wofy-1)));
		
		r = new SSLHistoryResultRequest(SSLHistoryResultRequest.ONSDAG_TYPE, 2007, 52);
		r = SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(r);
		r = SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(r);
		assertEquals(SSLHistoryResultRequest.ONSDAG_TYPE, r.getType());
		assertEquals(2008, r.getYear());
		assertEquals(1, r.getRound());
		
		r = new SSLHistoryResultRequest(SSLHistoryResultRequest.ONSDAG_TYPE, 2008, 52);
		r = SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(r);
		r = SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(r);
		assertEquals(SSLHistoryResultRequest.ONSDAG_TYPE, r.getType());
		assertEquals(2009, r.getYear());
		assertEquals(1, r.getRound());
		
		
		r = new SSLHistoryResultRequest(SSLHistoryResultRequest.ONSDAG_TYPE, 2009, 53);
		r = SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(r);
		r = SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(r);
		assertEquals(SSLHistoryResultRequest.ONSDAG_TYPE, r.getType());
		assertEquals(2010, r.getYear());
		assertEquals(1, r.getRound());
		
		
		r = new SSLHistoryResultRequest(SSLHistoryResultRequest.LORDAG_TYPE, 1983, 40);
		r = SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(r);
		assertEquals(SSLHistoryResultRequest.LORDAG_TYPE, r.getType());
		assertEquals(1983, r.getYear());
		assertEquals(41, r.getRound());
		
		r = new SSLHistoryResultRequest(SSLHistoryResultRequest.LORDAG_TYPE, 1998, 39);
		r = SSLHistoryResultsAnalyzer.getInstance().getNextResultRequest(r);
		assertEquals(SSLHistoryResultRequest.ONSDAG_TYPE, r.getType());
		assertEquals(1998, r.getYear());
		assertEquals(40, r.getRound());
	}
}
